//package com.lyt.springboot.filter;
//
//import com.lyt.springboot.Exception.UnPermissionException;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.Order;
////import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Component;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import javax.annotation.Resource;
//import javax.servlet.*;
//import javax.servlet.http.HttpServletRequest;
//import java.io.IOException;
//import java.util.concurrent.TimeUnit;
//import javax.servlet.annotation.WebFilter;
//
//
//@Slf4j
////@WebFilter(urlPatterns = "/*")
////@Component
////@Order(1)
//public class MyFilter implements Filter {
//
//    @Autowired
//    private StringRedisTemplate redisTemplate;
//
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//        Filter.super.init(filterConfig);
//        System.out.println("MyFilter过滤器成功初始化。。。。。。。。。。。。。");
//        Filter.super.init(filterConfig);
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        //获取访问 ip 地址
////        String ipAddr = getIpAddr(request);
//        //.append(ipAddr)
//        // 存入缓存10s不允许访问
//        String key = new StringBuilder().append("KKey").toString();
//
//        if (redisTemplate.hasKey(key)) {
//            // 访问次数自增1
//            redisTemplate.opsForValue().increment(key, 1);
//            String value = redisTemplate.opsForValue().get(key);
//            if (Integer.valueOf(value) > 10) {
//                throw new UnPermissionException();
//            }
//            log.warn("访问过快，存在强刷行为！key={}", key);
//        } else {
//            // 第一次访问
//            redisTemplate.opsForValue().set(key, "1", 10,
//                    TimeUnit.SECONDS);
//        }
//        try {
//            filterChain.doFilter(servletRequest, servletResponse);
//        } catch (Exception e) {
//            log.warn("认证失败，e:{},url:{},parameters:{}", e, request.getRequestURL(), request.getParameterMap());
//            servletResponse.setContentType("application/json");
//            servletResponse.setCharacterEncoding("UTF-8");
//        }
//    }
//
//    @Override
//    public void destroy() {
//        Filter.super.destroy();
//        System.out.println("MyFilter过滤器被销毁");
//    }
//
//    public static String getIpAddr(HttpServletRequest request) {
//        String ipAddress = null;
//        try {
//            ipAddress = request.getHeader("X-Forwarded-For");
//            if (ipAddress != null && ipAddress.length() != 0 && !"unknown".equalsIgnoreCase(ipAddress)) {
//                // 多次反向代理后会有多个ip值，第一个ip才是真实ip
//                if (ipAddress.indexOf(",") != -1) {
//                    ipAddress = ipAddress.split(",")[0];
//                }
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getHeader("Proxy-Client-IP");
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getHeader("WL-Proxy-Client-IP");
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getHeader("HTTP_CLIENT_IP");
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getRemoteAddr();
//            }
//        } catch (Exception e) {
//
//        }
//        return ipAddress;
//    }
//}
